package org.mx.dal.entity;


import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonIgnore;

import lombok.Getter;
import lombok.Setter;

/**
 * Geo Multi Point定义类
 * 
 * @author john.peng
 *
 */
@Getter
@Setter
public class GeoShapeMultiPoint extends GeoShape {

	private static final Logger logger = LoggerFactory.getLogger(GeoShapeMultiPoint.class);

	@JsonIgnore
	private List<GeoPoint> points = new ArrayList<>();

	/**
	 * 默认的构造函数
	 */
	public GeoShapeMultiPoint() {
		super();
		super.setType("MultiPoint");
	}

	private void fillData() {
		if (points.size() < 2) {
			if (logger.isWarnEnabled()) {
				logger.warn("The multi point need 2 points at least, the coordinates will be set to null.");
			}
			super.setCoordinates(new double[0][0]);
		} else {
			double[][] coordinates = new double[points.size()][2];
			for (int index = 0; index < points.size(); index++) {
				coordinates[index][0] = points.get(index).getLon();
				coordinates[index][1] = points.get(index).getLat();
			}
			super.setCoordinates(coordinates);
		}
	}

	/**
	 * 向Multi Point中添加一个点
	 * @param point 点
	 * @return Multi Point
	 */
	public GeoShapeMultiPoint addPoint(GeoPoint point) {
		points.add(point);
		fillData();
		return this;
	}

	/**
	 * 向Multi Point中添加几个点
	 * @param points 点集合
	 * @return Multi Point
	 */
	public GeoShapeMultiPoint addPoints(List<GeoPoint> points) {
		this.points.addAll(points);
		fillData();
		return this;
	}

	/**
	 * 清除Multi Point的点
	 * @return Multi Point
	 */
	public GeoShapeMultiPoint clear() {
		points.clear();
		fillData();
		return this;
	}}
